package com.okilo.online.app.configs

import com.okilo.online.app.BuildConfig
import com.okilo.online.app.lib.analyse.OkiloAnalyseUtil
import com.okilo.online.app.tools.OkiloDateUtil
import com.okilo.online.app.tools.shared.OkiloSharedHp
import com.okilo.online.app.tools.shared.OkiloSharedKey
import org.json.JSONObject
import java.util.Random
import java.util.TimeZone

/**
 * Configs
 * @Author wwdeng
 * @CreateTime: 2024/12/5 10:59
 * @Annotation：APP 配置
 */
object OkiloConfigs {

    /**
     * APP 名称
     */
    const val OKILO_APPNAME = BuildConfig.appName

    /**
     * 隐私协议（尼日尼亚） nigeria
     */
    const val OKILO_PP_NIGERIA_LINK = "https://sites.google.com/sukorg.com/okilo-loan"

    /**
     * Adjust
     */
    const val OKILO_ADJ_TOKEN = "qtfotuv2yiv4"

    /**
     * 获取加解密Key
     */
    fun gOkiloSecretKey(): String{
        return OkiloSharedHp.gOkiloSharedString(OkiloSharedKey.SP_OKILO_SECRET_KEY)
    }

    /**
     * 获取加解密IV
     */
    fun gOkiloSecretIv(): String{
        return OkiloSharedHp.gOkiloSharedString(OkiloSharedKey.SP_OKILO_SECRET_IV)
    }

    /**
     * 获取时区转发url
     * /v1/timezonePath
     */
    fun gOkiloTimeZoneUrl(): String{
        return "${BuildConfig.apiHost}/t061K5NFurCV8JX11xMp7YA"
    }

    /**
     * 客服、流程配置url
     * /v1/commonApi
     */
    fun gOkiloConfigCustomerUrl(): String{
        return gOkiloFormatUrl("/Ddrzwnjkv2Llse16POJ9ug")
    }

    /**
     * 用户url
     * /v1/commonApi
     */
    fun gOkiloUserUrl(): String{
        return gOkiloFormatUrl("/rWhsW4YJtraIzYo3Wisefj")
    }

    /**
     * 设备信息、数据分析
     * /v1/commonApi
     */
    fun gOkiloSubSdkUrl(): String{
        return gOkiloFormatUrl("/Ajqh0UNOeftxLfCnJp3yNQ")
    }

    /**
     * 申贷
     * /v1/commonApi
     */
    fun gOkiloApplyUrl(): String{
        return gOkiloFormatUrl("/WXnV4d0333ozqGeIJ9gt1y")
    }

    /**
     * 埋点
     * /v1/commonApi
     */
    fun gOkiloTrackPointsUrl(): String{
        return gOkiloFormatUrl("/squDgjUkXz2GF2QuTmT2wj")
    }

    /**
     * 图片上传url
     * /v1/imageUpload
     */
    fun gOkiloUploadFileUrl(): String{
        return gOkiloFormatUrl("/BsJZYKa4g0dVdJRqVbp8g")
    }

    /**
     * 通用接口url
     * /v1/commonApi
     */
    fun gOkiloRequestUrl(): String{
        return gOkiloFormatUrl("/WXnV4d0333ozqGeIJ9gt1y")
    }

    /**
     * 格式化url: host/timezone/path
     */
    private fun gOkiloFormatUrl(url: String): String{
        //地区转发
        val timeZone = gOkiloTimeZone()
        if (timeZone.isNotEmpty()){
            return "${BuildConfig.apiHost}/$timeZone$url"
        }
        return "${BuildConfig.apiHost}$url"
    }

    /**
     * 添加公共参数
     */
    fun JSONObject.addParams(): JSONObject {
        apply {
            put("product", BuildConfig.appName)                                           //  产品 APP名称
            put("token", gOkiloLoginToken())                                               //  token
            put("apiVersion","v3")                                                      //  接口版本
            put("androidId", OkiloAnalyseUtil.getAndroidId())                                    //  安卓id
            put("advertId", OkiloAnalyseUtil.getAdId())                                          //  广告id
            put("aduid", OkiloAnalyseUtil.getAdId())                                             //  广告id
            put("aduidPath", OkiloAnalyseUtil.getAndroidId())                                    //  广告地址
            put("isLimitAdTrackingEnabled", OkiloAnalyseUtil.getAdEnabled().toString())          //  是否限制广告id，1是 0否
            put("clientType","ANDROID")
            put("userId", gOkiloLoginUserId())                                             //  userId
            put("version", BuildConfig.VERSION_CODE)                                        //  app版本号
            put("timp", System.currentTimeMillis())                                            //  时间戳
            put("country", OkiloSharedHp.gOkiloSharedString(OkiloSharedKey.SP_OKILO_TIMEZONE_COUNTRY))                                  //国家标识
            put("tzid", TimeZone.getDefault().id)                                          //当前时区id
            put("localTimestamp","${System.currentTimeMillis()}")                       //时间戳
//            put("path","")                                                                        //  传映射后的接口路径，不带前面的/号
        }
        return this.addCustomParams()
    }

    /**
     * 添加随机参数增量 4
     */
    private fun JSONObject.addCustomParams(): JSONObject {
        put("loginMobile",gOkiloLoginMobile())
        when (Random().nextInt(4)) {
            1 -> put("startTime", gOkiloLaunchTime())
            2 -> {
                put("startTime", gOkiloLaunchTime())
                put("loginTime", gOkiloLoginTime())
            }
            3 -> {
                put("startTime", gOkiloLaunchTime())
                put("loginTime", gOkiloLoginTime())
                put("currentTime", gOkiloCurrentTime())
            }
        }
        return this
    }

    //启动时间
    private fun gOkiloLaunchTime() = OkiloSharedHp.gOkiloSharedString(OkiloSharedKey.SP_OKILO_APP_START_TIME)
    //登录时间
    private fun gOkiloLoginTime() = OkiloSharedHp.gOkiloSharedString(OkiloSharedKey.SP_OKILO_LOGIN_TIME)
    //登录手机号
    fun gOkiloLoginMobile() = OkiloSharedHp.gOkiloSharedString(OkiloSharedKey.SP_OKILO_MOBILE)
    //登录Token
    fun gOkiloLoginToken() = OkiloSharedHp.gOkiloSharedString(OkiloSharedKey.SP_OKILO_TOKEN)
    //登录用户id
    fun gOkiloLoginUserId() = OkiloSharedHp.gOkiloSharedString(OkiloSharedKey.SP_OKILO_USERID)
    //时区
    fun gOkiloTimeZone() = OkiloSharedHp.gOkiloSharedString(OkiloSharedKey.SP_OKILO_TIMEZONE)
    //当前时间
    private fun gOkiloCurrentTime() = OkiloDateUtil.getFormatTime(System.currentTimeMillis(), "dd/MM/yyyy HH:mm:ss")

    /**
     * 请求接口Path
     */
    fun gOkiloRequestPath(pathName: String): String{
        when(pathName){
            /**
             * APP配置参数 Path
             * /v1/getAppShowInfo
             */
            "APP_INFO" -> {
                return "IsKzq9l3ppNyObDHYMt12A"
            }
            /**
             * 发送whatsapp验证码
             * /v1/sendWhatAppMessage
             */
            "SEND_WHATAPP_CODE" -> {
                return "Ga8Ufb5OVCmf36rNT7wxMw"
            }
            /**
             * 校验whatsapp验证码
             *  /v1/checkWhatAppMessageCode
             */
            "CHECK_WHATAPP_CODE" -> {
                return "cpypjMuEBMYaex37TvyDJg"
            }
            /**
             * 无验证码登录
             * /v1/userLoginNoCode
             */
            "LOGIN" -> {
                return "iYVL8ayn7O6hj9juocqujQ"
            }
            /**
             * 用户流程状态
             * /v1/userProcess
             */
            "PROCESS" -> {
                return "gVZOLWqYPzyUDXXVsAtiMw"
            }
            /**
             * 用户贷款额度
             * /v1/loanLimit
             */
            "LIMIT" -> {
                return "CoAwdcCLuCxMtrNFx4MSQ"
            }
            /**
             * 新用户贷款额度预览信息
             * /v1/getLoanApplyDetail
             */
            "LOAN_DETAILS" -> {
                return "dsfVovRuV9LBOFgLFQQ"
            }
            /**
             * 可选额度和期限的预览信息(老用户)
             * /getLoanDetailPageForDays
             */
            "APPLY_DETAILS" -> {
                return "cxCtxfRVAVJUeDSnB05KEA"
            }
            /**
             * 创建贷款申请订单
             * /v1/loanApply
             */
            "APPLY_CREATE_ORDER" -> {
                return "EdAvmXJUoICGNAHfrXHZFA"
            }
            /**
             * 贷款还款信息
             * /v1/repayInfo
             */
            "REPAY_INFO" -> {
                return "HJ9GrRtVaKq5GHIRIfI6Ow"
            }
            /**
             * 贷款还款链接(贷超)
             * /v1/orderRepayH5
             */
            "REPAY_LINK" -> {
                return "t0lUrL7wlB9iqmVP7kZLg"
            }
            /**
             * 获取支付状态（没有还款链接时，会进入查询支付状态流程）
             * /v1/queryPaymentStatus
             */
            "PIN_PAY_STATUS" -> {
                return "TLGhIcWdM5Ro5jgSnjA7Fw"
            }
            /**
             * 账户列表
             * /v1/getBindCardList
             */
            "WALLET_LIST" -> {
                return "B9tEykb92zTFS0S8zKT0w"
            }
            /**
             * 评价弹框
             * /v1/fetchStarWindowConfig
             */
            "COMMENT_STAR" -> {
                return "Ojs2FtxNxz5JZ2jaLNmOA"
            }
            /**
             * 提交意见反馈
             * /v1/feedback
             */
            "SUB_FEEDBACK" -> {
                return "EqYomuOczV7AM3MutaPNQ"
            }
            /**
             * 在线客服会话记录
             * /v1/feedbackTalk
             */
            "CHAT_ONLINE" -> {
                return "weY2WDvQoqBlfIgyeN1okA"
            }
            /**
             * 事件批量埋点
             * v1/userBuriedPointForBatch
             */
            "TRACK_BATCH" -> {
                return "NPO6csz4HHeFLVsBatShNw"
            }
            /**
             * 保存自定义参数接口
             * /v1/saveAbUserInfo
             */
            "SAVE_CUSTOM_DATA" -> {
                return "cmO8x94U7D4DEf1cO7wWIw"
            }
            /**
             * 获取自定义参数
             * /v1/getAbUserInfo
             */
            "GET_CUSTOM_DATA" -> {
                return "pcTTzxG4M0g8pYE08NWUxQ"
            }
            /**
             * 清除用户数据
             * /v1/dropUserData
             */
            "CLEAR_USER_DATA" -> {
                return "vqxxZWsXysqM1IexYrjxw"
            }
            /**
             * 产品额度是否有提升
             * /v1/amountIsUp
             */
            "LIMIT_RAISE" -> {
                return "ViwkGr4udWGRjSiUdXsKKg"
            }
            /**
             * 设备数据上传状态
             * /v1/queryDeviceInfoTypes
             */
            "DEVICE_TYPES" -> {
                return "aJOObXnvQuyxuyBZTV6Chg"
            }
            /**
             * Adjust 安装数据上报
             * /v1/adjustInstallTemp
             */
            "ADJUST_INSTALL" -> {
                return "nGwNKxsoD0rGENSpa9PSA"
            }
            /**
             * Google 安装数据上报
             * /v1/channelInstallTemp
             */
            "GOOGLE_INSTALL" -> {
                return "aJB9tiBXqskbe9AQah34Q"
            }
            /**
             * firebase数据上报
             * /v1/fireBase/save
             */
            "FIREBASE" -> {
                return "m0gKCia9k6aLQHbEBLNCA"
            }
        }
        return ""
    }
}